﻿/*********************************************************************************/
/**    Sims3Pack Readme Generator                                               **/
/**    Copyright (C) 2012  Anja Knackstedt                                      **/
/**                                                                             **/
/**    This program is free software: you can redistribute it and/or modify     **/
/**    it under the terms of the GNU General Public License as published by     **/
/**    the Free Software Foundation, either version 3 of the License, or        **/
/**    (at your option) any later version.                                      **/
/**                                                                             **/
/**    This program is distributed in the hope that it will be useful,          **/
/**    but WITHOUT ANY WARRANTY; without even the implied warranty of           **/
/**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            **/
/**    GNU General Public License for more details.                             **/
/**                                                                             **/
/**    You should have received a copy of the GNU General Public License        **/
/**    along with this program.  If not, see <http://www.gnu.org/licenses/>.    **/
/**                                                                             **/
/*********************************************************************************/



using System;
using System.IO;
using System.Drawing;
using Sims3PackReadmeGenerator.language;

namespace Sims3PackReadmeGenerator.Sims3PackReader
{
    
    static class Sims3PackBinaryReader
    {
        #region static methods

        #region seperateXML

        /// <summary>
        /// seperates the xml part from a sims3pack file
        /// </summary>
        /// <param name="filePath">filepath</param>
        /// <returns>XML-Teil</returns>
        public static string getXMLPart(string filePath, out int binaryStartPosition)
        {
            // DWORD: 4 Bytes
            // WORD: 2 Bytes

            // Sims3Pack
            // DWORD length of the following string
            // String
            // WORD
            // DWORD length of the following xml part

            try
            {
                string XMLPart = "";

                BinaryReader binaryReader = new BinaryReader(new FileStream(filePath, FileMode.Open, FileAccess.Read));

                //read 1. DWORD 
                byte[] DWordLength = binaryReader.ReadBytes(4);
                int stringLength = getLengthFromDWord(DWordLength);

                //skip string part and WORD         
                binaryReader.BaseStream.Position += stringLength + 2;

                //read 2. DWORD 
                DWordLength = binaryReader.ReadBytes(4);
                int xmlLength = getLengthFromDWord(DWordLength);

                // read XML part and convert it
                byte[] XMLBytes = binaryReader.ReadBytes(xmlLength);
                XMLPart = System.Text.UTF8Encoding.UTF8.GetString(XMLBytes);

                binaryStartPosition = 4 + stringLength + 2 + 4 + xmlLength;

                binaryReader.Close();

                return XMLPart;
            }
            catch(Exception)
            {
               
                throw;
            }
            
             
            
        }

        /// <summary>
        /// gets length from a DWORD
        /// e.g. 4C 0A 00 00 -> A4C
        /// </summary>
        /// <param name="dword">DWord as Byte Array (4 Bytes)</param>
        /// <returns>length</returns>
        private static int getLengthFromDWord(byte[] dword)
        {
            int lenght = 0;

            if (dword.Length != 4)
            {
                throw new Exception(errors.errorBinarayReaderExceptionInvaildFormat);
            }
            else
            {
                int byte0 = dword[0];
                int byte1 = dword[1] * 0x100;
                int byte2 = dword[2] * 0x10000;
                int byte3 = dword[3] * 0x1000000;

                lenght = byte0 + byte1 + byte2 + byte3;
            }


            return lenght;
        }

        #endregion

        #region png

        /// <summary>
        /// gets a thumbnail from a sims3pack file
        /// </summary>
        /// <param name="filePath">path of the sims3pack</param>
        /// <param name="offset">offset of the thumb in the file</param>
        /// <param name="length">length of the thumbnail</param>
        /// <param name="binaryStartPosition">startposition of the dbpf</param>
        /// <returns></returns>
        public static Bitmap getThumbnail(string filePath, int offset, int length, int binaryStartPosition)
        {
            try
            {
                BinaryReader binaryReader = new BinaryReader(new FileStream(filePath, FileMode.Open, FileAccess.Read));


                binaryReader.BaseStream.Position = binaryStartPosition + offset;
                Byte[] pngBytes = binaryReader.ReadBytes(length);

                MemoryStream str = new MemoryStream(pngBytes);
                Bitmap bmp = new Bitmap(str);
                binaryReader.Close();

                return bmp;
            }
            catch(Exception)
            {
                throw;
            }
        }

        #endregion

        #endregion
    }
}
